package com.example.tests;

import com.example.framework.GroupData;
import com.example.utils.SortedListOf;
import org.testng.annotations.Test;

import java.util.Random;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

public class GroupRemovingTest extends TestManager{
    Random rnd = new Random();

    @Test
    public void shouldRemoveGroup(){
        // save old state
        SortedListOf<GroupData> oldGroupList = new SortedListOf<GroupData>(manager.getHibernateHelper().listGroups());

        // actions
        int index = rnd.nextInt(oldGroupList.size() - 1);
        manager.getGroupHelper().deleteGroupByIndex(index);

        // save new state
        SortedListOf<GroupData> newGroupList = manager.getGroupHelper().getGroups();

        // compare states
        assertThat(newGroupList, equalTo(oldGroupList.without(index)));
    }

    @Test
    public void shouldRemoveSeveralGroups(){
        // save old state
        SortedListOf<GroupData> oldGroupList = new SortedListOf<GroupData>(manager.getHibernateHelper().listGroups());

        // actions
        int index1 = rnd.nextInt(oldGroupList.size() - 1);
        GroupData group1 = manager.getGroupHelper().getGroupByIndex(index1);
        int index2 = rnd.nextInt(oldGroupList.size() - 1);
        while (index2 == index1){
            index2 = rnd.nextInt(oldGroupList.size() - 1);
        }
        GroupData group2 = manager.getGroupHelper().getGroupByIndex(index2);

        manager.getGroupHelper()
            .selectGroupByIndex(index1)
            .selectGroupByIndex(index2)
            .submitRemoving();

        // save new state
        SortedListOf<GroupData> newGroupList = manager.getGroupHelper().getGroups();

        // compare states
        assertThat(newGroupList, equalTo(oldGroupList.without(group1).without(group2)));
    }
}
